/*
 * Copyright (c) 2021, 2022, yzfh.cn All rights reserved.
 *
 */
package com.ly.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.mp.mapper.UserMapper;
import com.ly.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * @author yzfh
 * @Date 2022/02/24 星期四 10:50:28
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest2 {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testInsert(){
        User user = new User();
        user.setUserName("caocao");
        user.setPassword("123456");
        user.setName("曹操1");
        user.setAge(31);
        user.setMail("2@ly.com");
        user.setAddress("北京");

        int row = this.userMapper.insert(user);

        System.out.println(row);

        System.out.println(user.getId());

    }

    @Test
    public void testUpdateById() {
        User user  = new User();
        user.setId(6L);
        user.setAge(21);

        int row = this.userMapper.updateById(user);
        System.out.println(row);
    }

    @Test
    public void testUpdate(){
        /*//通过user更新
        User user = new User();
        user.setAge(22);*/

//        //更新条件
//        QueryWrapper<User> wrapper = new QueryWrapper<>();
//        wrapper.eq("id",7L);
//
//        int row = this.userMapper.update(user, wrapper);
//        System.out.println(row);

        //更新条件及字段
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",8L).set("age",23);

        int row = this.userMapper.update(null, wrapper);
        System.out.println(row);

    }

    @Test
    public void testDeleteById(){
        int row = this.userMapper.deleteById(9L);
        System.out.println(row);
    }

    @Test
    public void testDeleteByMap() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("age",23);
        hashMap.put("name","曹操1");

        int row = this.userMapper.deleteByMap(hashMap);
        System.out.println(row);
    }

    @Test
    public void testDelete(){
        User user = new User();
        user.setAge(22);
        user.setName("曹操1");

        //将实体对象进行封装，封装为操作条件
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);

        int row = this.userMapper.delete(wrapper);
        System.out.println(row);

    }

    @Test
    public void testDeleteBatchIds(){
        int i = this.userMapper.deleteBatchIds(Arrays.asList(6L, 10L));
        System.out.println(i);
    }

    @Test
    public void testSelectById() {
        User user = this.userMapper.selectById(1L);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds(){
        List<User> userList = this.userMapper.selectBatchIds(Arrays.asList(1L, 2L, 3L, 4L, 5L));
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectOne() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","张三");

        //根据条件查询一条数据
        User user = this.userMapper.selectOne(wrapper);
        System.out.println(user);
    }

    @Test
    public void testSelectCount() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age",20);

        //根据条件查询记录总数
        Long count = this.userMapper.selectCount(wrapper);
        System.out.println(count);
    }

    @Test
    public void testSelectList(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age",20);

        //根据条件查询记录集合
        List<User> userList = this.userMapper.selectList(wrapper);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void testSelectPage(){
        Page<User> page = new Page<>(1L,1L);

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age",20);

        IPage<User> iPage = this.userMapper.selectPage(page, wrapper);
        System.out.println("数据总条数："+iPage.getTotal());
        System.out.println("总页数："+iPage.getPages());

        List<User> userList = iPage.getRecords();
        for (User user : userList) {
            System.out.println("user =>"+user);
        }

    }

}
